package classes.controller
{
	import com.greensock.TweenMax;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	public class SoundHandler extends EventDispatcher
	{
		static private var _soundHandleInstance:SoundHandler;
		public function SoundHandler(singletonEnforcer:SingletonEnforcer){}
		
		private var _bgmSound:Sound;
		private var _effectSound:Sound;
		
		public static function getSoundInstance():SoundHandler
		{
			if(SoundHandler._soundHandleInstance == null){
				SoundHandler._soundHandleInstance = new SoundHandler(new SingletonEnforcer());	
			}
			return SoundHandler._soundHandleInstance;
		}
		
		//BGM sound 최초 로드
		private var _soundChannel_bgm:SoundChannel;
		private var _soundTransform_bgm:SoundTransform;
		public function createSoundBgm($url:String, $firstVolume:int):void
		{
			if(_bgmSound == null){
				_bgmSound	   						= new Sound(new URLRequest($url));
				_soundChannel_bgm 					= _bgmSound.play(0, int.MAX_VALUE);
				
				_soundTransform_bgm					= new SoundTransform(0, 0);
				_soundChannel_bgm.soundTransform 	= _soundTransform_bgm;
				
				volumeController(0, 0, 0.5);
			}
		}
		//EFFECT sound 최초 로드
		private var _soundChannel_effect:SoundChannel;
		private var _soundTransform_effect:SoundTransform;
		public function createSoundEffect($url:String, $firstVolume:int):void
		{
			if(_effectSound == null){
				_effectSound	   					= new Sound(new URLRequest($url));
				_soundChannel_effect 				= _effectSound.play(0, int.MAX_VALUE);
				
				_soundTransform_effect				= new SoundTransform(0, 0);
				_soundChannel_effect.soundTransform = _soundTransform_effect;
				volumeController(1, 0, 0.5);
			}
		}
		
		public function volumeController($channel:int, $delay:int, $volume:int):void
		{
			if($channel==0){
				TweenMax.to(_soundChannel_bgm, $delay, {volume:$volume});	
			}else{
				TweenMax.to(_soundChannel_effect, $delay, {volume:$volume});
			}
		}
	
	}
}


class SingletonEnforcer{}